package com.it.schoolhomeos.controller;

import com.it.schoolhomeos.dto.ApiResponse;
import com.it.schoolhomeos.entity.Exam;
import com.it.schoolhomeos.service.ExamService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 考试控制器
 * 
 * 提供考试相关的API接口
 */
@RestController
@RequestMapping("/api/exams")
@Tag(name = "考试管理", description = "考试相关的API接口")
public class ExamController {

    @Autowired
    private ExamService examService;
    
    /**
     * 获取所有考试
     * 
     * @return 考试列表
     */
    @GetMapping
    @Operation(summary = "获取所有考试", description = "获取所有考试信息")
    public ApiResponse<List<Exam>> getAllExams() {
        List<Exam> exams = examService.getAllExams();
        return ApiResponse.success(exams);
    }
    
    /**
     * 分页获取考试
     * 
     * @param page 页码
     * @param size 每页大小
     * @return 分页的考试列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页获取考试", description = "分页获取考试信息")
    public ApiResponse<Page<Exam>> getExams(
            @Parameter(description = "页码") @RequestParam(defaultValue = "0") int page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int size) {
        Page<Exam> exams = examService.getExams(page, size);
        return ApiResponse.success(exams);
    }
    
    /**
     * 根据ID获取考试
     * 
     * @param id 考试ID
     * @return 考试对象
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取考试", description = "根据ID获取考试信息")
    public ApiResponse<Exam> getExamById(@PathVariable Long id) {
        Exam exam = examService.getExamById(id);
        return ApiResponse.success(exam);
    }
    
    /**
     * 根据名称查找考试
     * 
     * @param name 考试名称
     * @return 考试列表
     */
    @GetMapping("/by-name")
    @Operation(summary = "根据名称查找考试", description = "根据名称查找考试信息")
    public ApiResponse<List<Exam>> findByName(
            @Parameter(description = "考试名称") @RequestParam String name) {
        List<Exam> exams = examService.findByName(name);
        return ApiResponse.success(exams);
    }
    
    /**
     * 根据日期查找考试
     * 
     * @param date 考试日期
     * @return 考试列表
     */
    @GetMapping("/by-date")
    @Operation(summary = "根据日期查找考试", description = "根据日期查找考试信息")
    public ApiResponse<List<Exam>> findByDate(
            @Parameter(description = "考试日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        List<Exam> exams = examService.findByDate(date);
        return ApiResponse.success(exams);
    }
    
    /**
     * 根据日期范围查找考试
     * 
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 考试列表
     */
    @GetMapping("/by-date-range")
    @Operation(summary = "根据日期范围查找考试", description = "根据日期范围查找考试信息")
    public ApiResponse<List<Exam>> findByDateRange(
            @Parameter(description = "开始日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        List<Exam> exams = examService.findByDateRange(startDate, endDate);
        return ApiResponse.success(exams);
    }
    
    /**
     * 根据科目查找考试
     * 
     * @param subject 科目
     * @return 考试列表
     */
    @GetMapping("/by-subject")
    @Operation(summary = "根据科目查找考试", description = "根据科目查找考试信息")
    public ApiResponse<List<Exam>> findBySubject(
            @Parameter(description = "科目") @RequestParam String subject) {
        List<Exam> exams = examService.findBySubject(subject);
        return ApiResponse.success(exams);
    }
    
    /**
     * 获取考试的科目列表
     * 
     * @param id 考试ID
     * @return 科目列表
     */
    @GetMapping("/{id}/subjects")
    @Operation(summary = "获取考试的科目列表", description = "获取指定考试的科目列表")
    public ApiResponse<List<String>> getExamSubjects(@PathVariable Long id) {
        List<String> subjects = examService.getExamSubjects(id);
        return ApiResponse.success(subjects);
    }
    
    /**
     * 创建考试
     * 
     * @param exam 考试对象
     * @return 创建后的考试对象
     */
    @PostMapping
    @Operation(summary = "创建考试", description = "创建新的考试")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<Exam> createExam(@RequestBody Exam exam) {
        Exam createdExam = examService.createExam(exam);
        return ApiResponse.success(createdExam);
    }
    
    /**
     * 更新考试
     * 
     * @param id 考试ID
     * @param exam 考试对象
     * @return 更新后的考试对象
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新考试", description = "更新指定的考试")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<Exam> updateExam(@PathVariable Long id, @RequestBody Exam exam) {
        exam.setId(id);
        Exam updatedExam = examService.updateExam(exam);
        return ApiResponse.success(updatedExam);
    }
    
    /**
     * 删除考试
     * 
     * @param id 考试ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除考试", description = "删除指定的考试")
    @PreAuthorize("hasRole('TEACHER') or hasRole('ADMIN')")
    public ApiResponse<Void> deleteExam(@PathVariable Long id) {
        examService.deleteExam(id);
        return ApiResponse.success("删除考试成功");
    }
} 